<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/26 0026
 * Time: 下午 3:21
 */
namespace app\api\controller;

use app\common\Helper;
use app\common\model\Storelabel;
use app\common\model\Storelabelconnect;
use think\Db;
use think\Exception;
use think\Request;

class Label extends Base{
    //创建店铺标签
    public function saveLabel(Request $request){
        $param = $request->param();
        if(empty($param['storeId'])){
            return show(config('status.ERROR_STATUS'),'参数错误','参数错误');
        }
        if(empty($param['storeLabel'])){
            return show(config('status.ERROR_STATUS'),'标签不能为空','标签不能为空');
        }
        Db::startTrans();
        try{
            $Storelabelconnect = Storelabelconnect::where('storeId',$param['storeId'])->select();
            if($Storelabelconnect){
                foreach ($Storelabelconnect as $v){
                    $Storelabel = Storelabel::get($v['storeLabelId']);
                    if($param['storeLabel'] == $Storelabel->storeLabel){
                        return show(config('status.ERROR_STATUS'),'不能重复添加相同的标签','不能重复添加相同的标签');
                    }
                }
            }
            $StorelabelModel = new Storelabel();
            $id = Helper::getUUID();
            $StorelabelModel->id = $id;
            $StorelabelModel->storeLabel = $param['storeLabel'];
            $StorelabelModel->createOperator = $param['userId'];
            $StorelabelModel->save();

            $StorelabelconnectModel = new Storelabelconnect();
            $StorelabelconnectModel->storeId = $param['storeId'];
            $StorelabelconnectModel->storeLabelId = $id;
            $StorelabelconnectModel->save();
            Db::commit();
            return show(config('status.SUCCESS_STATUS'),'添加成功','添加成功');
        }catch (Exception $ex){
            Db::rollback();
            return show(config('status.ERROR_STATUS'),'添加失败','添加失败');
        }

    }

    //获取店铺标签
    public function getAllLabel(Request $request){
        $param = $request->param();
        if(empty($param['storeId'])){
            return show(config('status.ERROR_STATUS'),'参数错误','参数错误');
        }
        $Storelabelconnect = Storelabelconnect::where('storeId',$param['storeId'])->select();
        if($Storelabelconnect){
            $lab = [];
            foreach ($Storelabelconnect as $v){
                $Storelabel = Storelabel::get($v['storeLabelId']);
                array_push($lab,$Storelabel);
            }
            return show(config('status.SUCCESS_STATUS'),'ok',$lab);
        }else{
            return show(2,'该店铺没有创建标签',NULL);
        }
    }

    //删除店铺标签
    public function deleteLabel(Request $request){
        $param = $request->param();
        if(empty($param['storeLabelId'])){
            return show(config('status.ERROR_STATUS'),'参数错误','参数错误');
        }

        Db::startTrans();
        try{
            $Storelabelconnect = Storelabelconnect::where('storeLabelId',$param['storeLabelId'])->find();
            $Storelabelconnect->delete();
            $Storelabel = Storelabel::get($param['storeLabelId']);
            $Storelabel->delete();
            Db::commit();
            return show(config('status.SUCCESS_STATUS'),'ok',1);
        }catch (Exception $ex){
            Db::rollback();
        }
    }
}